//
//  BMPhotoIOPhotoModel.m
//  bigmama
//
//  Created by huyuchen on 2017/9/28.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMPhotoIOPhotoModel.h"
#import "BMPhotoIOTool.h"
#import <CoreLocation/CoreLocation.h>

@interface BMPhotoIOPhotoModel ()
// 位置
@property (nonatomic, strong) NSString *locationString;
@end

@implementation BMPhotoIOPhotoModel

- (NSString *)createDateString {
    if (!_createDateString.length) {
        _createDateString = @"";
        if (self.createDate && [self.createDate isKindOfClass:[NSDate class]]) {
            _createDateString = [self.createDate formatYearMonthDayChinese_lib];
        }
    }
    return _createDateString;
}

- (void)setSelected:(BOOL)selected
{
    _selected = selected;
    // 图片选中状态才懒加载高清图
    if (selected && self.type == BMPhotoIOPhotoTypePhoto) {
        //        // 判断有误高清图, 没有则获取
        //        if (!self.originalImage) {
        //            if (!IOS8_bb) {
        //                self.originalImage = [[BBPhotoTool_lib sharedTool] originalImageWithAssetInIOS7:self.assetIOS7];
        //            }
        //        }
        // 获取缓存图,iOS8才有
        if (!self.cacheImage) {
            [self getCacheImage];
        }
    }
}

- (UIImage *)cacheImage
{
    // ios7用缩略图
    if (!_cacheImage && !IOS8) {
        _cacheImage = self.thumbImage;
    }
    return _cacheImage;
}

- (void)getCacheImage
{
    if (IOS8 && !self.cacheImage) {
        BMWeakSelf
        [[BMPhotoIOTool sharedTool] cacheImageWithAsset:self.asset finishBlock:^(id obj) {
            weakSelf.cacheImage = obj;
        }];
    }
}

- (void)getThumbImage_BM{
    if (IOS8 && !self.thumbImage) {
        BMWeakSelf
        [[BMPhotoIOTool sharedTool] thumbImageWithAsset:self.asset finishBlock:^(id obj) {
            weakSelf.thumbImage = obj;
        }];
    }
}

- (void)getOriginalImage_BM{
    if (IOS8 && !self.originalImage) {
        BMWeakSelf
        [[BMPhotoIOTool sharedTool] originalImageWithAsset:self.asset finishBlock:^(id obj) {
            weakSelf.originalImage = obj;
        }];
    }
}

- (PHAsset *)asset
{
    if (!_asset) {
        // 缓存的时候只有id，没有asset，要先根据id获取asset
        if (self.assetID.length) {
            _asset = [[BMPhotoIOTool sharedTool] getAssetWithID:self.assetID];
        }
    }
    return _asset;
}

// 获取地址
- (void)getLocationNameBlock:(void (^)(id obj))block
{
    if (!block) {
        block(@"");
        return;
    }
    
    // 获取过则使用缓存的位置
    if (self.locationString.length) {
        block(self.locationString);
    } else {
        // 获取位置
        // 获取具体位置
        BMWeakSelf
        
        // 没有位置，返回空字符串
        if (!self.location) {
            self.locationString = @"";
        }
        
        // 根据经纬度反向地理编译出地址信息
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:self.location completionHandler:^(NSArray *array, NSError *error){
            if (array.count > 0){
                CLPlacemark *placemark = array.firstObject;
                weakSelf.locationString = placemark.name;
                block(placemark.name);
            } else {
                weakSelf.locationString = @"";
                block(@"");
            }
        }];
        
    }
}

+ (instancetype)cameraModel
{
    BMPhotoIOPhotoModel *model = [[BMPhotoIOPhotoModel alloc] init];
    model.thumbImage = [UIImage imageNamed:@"pic_camera"];
    model.type = BMPhotoIOPhotoTypeCamera;
    model.createDate = [NSDate date];
    return model;
}
@end
